import os
import time

def do_clear_jar(directory,file_pre,is_remove):
    # 获取以 'bq-web-acs_' 为前缀的文件列表
    files = [f for f in os.listdir(directory) if f.startswith(file_pre)]
    # 获取每个文件的完整路径
    file_paths = [os.path.join(directory, f) for f in files]
    # 排序文件，按文件修改时间升序（最老的文件排在前面）
    file_paths.sort(key=lambda x: os.path.getmtime(x))
    # 删除除了最新两个版本外的所有文件
    for file_path in file_paths[:-2]:
        if is_remove:
            os.remove(file_path)
            print(f"已删除文件: {file_path}")
        else:
            print(f"即将删除文件: {file_path}")

if __name__ == '__main__':
    pre_list = {"bq-service-abms_","bq-service-cache_","bq-service-market_","bq-service-sso_","bq-service-gateway_",
                "bq-web-abms_","bq-web-abp_","bq-web-acs_","bq-web-fprocess_","bq-db-sync_"}
    directory = '/java/current/'
    for file_pre in pre_list:
        do_clear_jar(directory,file_pre,True)



